package com.ruoyi.rocky.service.impl;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.rocky.domain.PcExamine;
import com.ruoyi.rocky.domain.PcVehiclesJoin;
import com.ruoyi.rocky.mapper.PcVehiclesJoinMapper;
import com.ruoyi.rocky.mapper.PcVehiclesMapper;
import com.ruoyi.rocky.domain.PcVehicles;
import com.ruoyi.rocky.service.PcVehiclesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

/**
 * 车辆信息Service业务层处理
 * @date 2024-04-25
 */
@Service
public class PcVehiclesServiceImpl implements PcVehiclesService {
    @Autowired
    private PcVehiclesMapper pcVehiclesMapper;

    @Autowired
    private PcVehiclesJoinMapper pcVehiclesJoinMapper;

    /**
     * 查询车辆信息
     *
     * @param id 车辆信息主键
     * @return 车辆信息
     */
    @Override
    public PcVehicles selectPcVehiclesById(Long id) {
        return pcVehiclesMapper.selectPcVehiclesById(id);
    }

    /**
     * 查询车辆信息列表
     *
     * @param pcVehicles 车辆信息
     * @return 车辆信息
     */
    @Override
    public List<PcVehicles> selectPcVehiclesList(PcVehicles pcVehicles) {
        List<PcVehicles> pcVehicles1 = pcVehiclesMapper.selectPcVehiclesList(pcVehicles);
        return pcVehicles1;
    }

    /**
     * 申请车辆
     *
     * @param pcVehiclesJoin 车辆信息
     * @return 结果
     */
    @Override
    public R insertPcVehicles(PcVehiclesJoin pcVehiclesJoin) {
        PcVehicles pcVehicles = pcVehiclesMapper.selectPcVehiclesById(pcVehiclesJoin.getId());
        if (pcVehicles == null){
            PcVehiclesJoin pcVehiclesJoin1 = pcVehiclesJoinMapper.selectPcVehiclesJoinById(pcVehiclesJoin.getId());
            if (pcVehiclesJoin1==null){
                pcVehiclesJoinMapper.insertPcVehiclesJoin(pcVehiclesJoin);
            } else if(pcVehiclesJoin1.getApplyType() == 0) {
                return R.fail("审核中，请勿重复提交");
            }
        }else {
            return R.fail("已有车辆，不能申请！");
        }
        return R.ok("申请已提交！");
    }

    /**
     * 审核车辆
     * @param pcExamine
     * @return
     */
    @Transactional
    @Override
    public R examineStore(PcExamine pcExamine) {
        PcVehiclesJoin pcVehiclesJoin = pcVehiclesJoinMapper.selectPcVehiclesJoinById(pcExamine.getId());
        if (pcExamine.getApplyType()==2){
            pcVehiclesJoin.setReason(pcExamine.getReason());
            pcVehiclesJoin.setApplyType(pcExamine.getApplyType());
        }else {
            pcVehiclesJoin.setApplyType(pcExamine.getApplyType());
            PcVehicles pcVehicles = new PcVehicles();
            pcVehicles.setId(pcVehiclesJoin.getId());
            pcVehicles.setUserId(pcVehiclesJoin.getUserId());
            pcVehicles.setBrand(pcVehiclesJoin.getBrand());
            pcVehicles.setVehiclePhoto(pcVehiclesJoin.getVehiclePhoto());
            pcVehicles.setModel(pcVehiclesJoin.getModel());
            pcVehicles.setLicensePlate(pcVehiclesJoin.getLicensePlate());
            pcVehicles.setColor(pcVehiclesJoin.getColor());
            pcVehicles.setVinPhoto(pcVehiclesJoin.getVinPhoto());
            pcVehicles.setRegistrationPhoto(pcVehiclesJoin.getRegistrationPhoto());
            pcVehiclesMapper.insertPcVehicles(pcVehicles);
        }
        pcVehiclesJoinMapper.updatePcVehiclesJoin(pcVehiclesJoin);
        return R.ok();
    }


    /**
     * 修改车辆信息
     *
     * @param pcVehicles 车辆信息
     * @return 结果
     */
    @Override
    public R<Boolean> updatePcVehicles(PcVehicles pcVehicles) {
        pcVehicles.setUpdateTime(DateUtils.getNowDate());
        return pcVehiclesMapper.updatePcVehicles(pcVehicles)>0?R.ok():R.fail();
    }

    /**
     * 批量删除车辆信息
     *
     * @param ids 需要删除的车辆信息主键
     * @return 结果
     */
    @Override
    public R<Boolean> deletePcVehiclesByIds(Long[] ids) {
        return pcVehiclesMapper.deletePcVehiclesByIds(ids)>0?R.ok():R.fail();
    }

    /**
     * 删除车辆信息信息
     *
     * @param id 车辆信息主键
     * @return 结果
     */
    @Override
    public R<Boolean> deletePcVehiclesById(Long id) {
        return pcVehiclesMapper.deletePcVehiclesById(id)>0?R.ok():R.fail();
    }
}
